﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using Unijovem.UI.Helpers;
using Unijovem.UI.Models;

namespace Unijovem.UI.Controllers
{
    public class NoticiaController : Session
    {
        public ActionResult Detalhes(string guid)
        {
            try
            {
                var noticias = WebSession<List<NoticiaModel>>(SessionName.Noticias);
                var noticia = noticias.FirstOrDefault(n => n.Guid.ToString() == guid);

                if (noticia == null) return RedirectToAction("Index", "Home");

                return View(noticia);
            }
            catch
            {
                return RedirectToAction("Index", "Noticia");
            }
        }

        public ActionResult Index()
        {
            List<NoticiaModel> model = SelecionarNoticias();
            WebSession(SessionName.Noticias, model);
            return View(model);
        }

        public ActionResult Home()
        {
            try
            {
                List<NoticiaModel> model = SelecionarNoticias().Take(3).ToList();
                WebSession(SessionName.Noticias, model);
                return PartialView(model);
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
        }

        private List<NoticiaModel> SelecionarNoticias()
        {
            var myRequest = WebRequest.Create("http://ultimosegundo.ig.com.br/p1237569153183.xml");
            var myResponse = myRequest.GetResponse();

            var rssStream = myResponse.GetResponseStream();
            var rssDoc = new XmlDocument();
            rssDoc.Load(rssStream);

            var rssItems = rssDoc.SelectNodes("rss/channel/item");

            return rssItems.Cast<object>().Select((t, i) => PreencherNoticia(i, rssItems)).ToList();
        }

        private NoticiaModel PreencherNoticia(int i, XmlNodeList rssItems)
        {
            var noticia = new NoticiaModel();
            var item = rssItems.Item(i);

            XmlNode rssDetail = item.SelectSingleNode("title");
            noticia.Titulo = rssDetail != null ? HttpUtility.HtmlDecode(rssDetail.InnerText) : string.Empty;

            rssDetail = item.SelectSingleNode("link");
            noticia.Link = rssDetail != null ? rssDetail.InnerText : string.Empty;

            rssDetail = item.SelectSingleNode("description");
            noticia.Descricao = rssDetail != null ? HttpUtility.HtmlDecode(rssDetail.InnerText) : string.Empty;
            noticia.Autor = noticia.Descricao.Split('<')[1].Replace("p>", string.Empty);
            noticia.SubTitulo = noticia.Descricao.Split('<')[2].Replace("/p>", string.Empty);

            noticia.Descricao = noticia.Descricao.Replace(string.Concat("<p>", noticia.Autor, "</p>"), string.Empty);
            noticia.Descricao = noticia.Descricao.Replace(noticia.SubTitulo, string.Empty);
            noticia.Descricao = noticia.Descricao.Replace("<a", "<a target='_blank'");

            rssDetail = item.SelectSingleNode("pubDate");
            noticia.Data = rssDetail != null ? DateTime.Parse(rssDetail.InnerText).AddHours(5) : DateTime.MinValue;

            noticia.Guid = Guid.NewGuid();
            return noticia;
        }
    }
}
